package com.nd.android.musicboxlibrary.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.EditText;
import com.nd.android.musicboxlibrary.business.comm.MagicNumber;

/**
 * 
 * <br>
 * Created 2014年9月15日 下午11:04:12
 * 
 * @version
 * @author wat.ztag
 * 
 * @see
 */
public class CustomEditText extends EditText implements MagicNumber {

    /**
     * 
     * <br>Created 2014年11月28日 上午12:05:46
     * @author       wat.ztag
    */
    @Override
    public void initMagicNumber() {}
    
    private Drawable drawableRight;

    private Drawable drawableLeft;

    private Drawable drawableTop;

    private Drawable drawableBottom;

    int actionX, actionY;

    int mHeight = 0;

    private DrawableClickListener clickListener;

    /**
     * Creates a new instance of CustomEditText. <br>
     * Created 2014年9月16日 上午12:13:46
     * 
     * @param context Context
     */
    public CustomEditText(Context context) {
        super(context);
    }

    /**
     * Creates a new instance of CustomEditText. <br>
     * Created 2014年9月15日 下午11:04:29
     * 
     * @param context Context
     * @param attrs AttributeSet
     */
    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        // this Contructure required when you are using this view in xml
    }

    /**
     * Creates a new instance of CustomEditText. <br>
     * Created 2014年9月15日 下午11:04:40
     * 
     * @param context Context
     * @param attrs AttributeSet
     * @param defStyle int
     */
    public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * 
     * <br>
     * Created 2014年9月15日 下午11:04:44
     * 
     * @param canvas Canvas
     * @author wat.ztag
     */
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mHeight = h;
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
        if (left != null) {
            drawableLeft = left;
        }
        if (right != null) {
            drawableRight = right;
        }
        if (top != null) {
            drawableTop = top;
        }
        if (bottom != null) {
            drawableBottom = bottom;
        }

        super.setCompoundDrawables(left, top, right, bottom);
    }

    /**
     * 
     * <br>
     * Created 2014年9月15日 下午11:04:48
     * 
     * @param right Drawable
     * @author wat.ztag
     */
    public void setRightDrawables(Drawable right) {
        if (right != null) {
            drawableRight = right;
        }
        super.setCompoundDrawables(drawableLeft, drawableTop, right, drawableBottom);
    }

    /**
     * 
     * <br>
     * Created 2014年9月15日 下午11:04:53
     * 
     * @param left Drawable
     * @author wat.ztag
     */
    public void setLeftDrawables(Drawable left) {
        if (left != null) {
            drawableLeft = left;
        }
        super.setCompoundDrawables(left, drawableTop, drawableRight, drawableBottom);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Rect bounds;
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            actionX = (int) event.getX();
            actionY = (int) event.getY();
            if (drawableBottom != null && drawableBottom.getBounds().contains(actionX, actionY)) {
                clickListener.onClick(DrawableClickListener.DrawablePosition.BOTTOM);
                return super.onTouchEvent(event);
            }

            if (drawableTop != null && drawableTop.getBounds().contains(actionX, actionY)) {
                clickListener.onClick(DrawableClickListener.DrawablePosition.TOP);
                return super.onTouchEvent(event);
            }

            // this works for left since container shares 0,0 origin with bounds
            if (drawableLeft != null) {
                bounds = null;
                bounds = drawableLeft.getBounds();

                int x, y;
                int extraTapArea = (int) (n13 * getResources().getDisplayMetrics().density + f0_5);

                x = actionX;
                y = actionY;

                if (!bounds.contains(actionX, actionY)) {
                    /** Gives the +20 area for tapping. */
                    x = (int) (actionX - extraTapArea);
                    y = (int) (actionY - extraTapArea);

                    if (x <= 0) {
                        x = actionX;
                    }
                    if (y <= 0) {
                        y = actionY;
                    }
                    /** Creates square from the smallest value */
                    if (x < y) {
                        y = x;
                    }
                }

                if (bounds.contains(x, y) && clickListener != null) {
                    clickListener.onClick(DrawableClickListener.DrawablePosition.LEFT);
                    event.setAction(MotionEvent.ACTION_CANCEL);
                    return false;

                }
            }

            if (drawableRight != null) {

                bounds = null;
                bounds = drawableRight.getBounds();
                int x, y;
                int extraTapArea = n13;

                x = (int) (actionX + extraTapArea);
                y = (int) (actionY - extraTapArea);

                x = getWidth() - x;
                /*
                 * x can be negative if user taps at x co-ordinate just near the width. e.g views
                 * width = 300 and user taps 290. Then as per previous calculation 290 + 13 = 303.
                 * So subtract X from getWidth() will result in negative value. So to avoid this add
                 * the value previous added when x goes negative.
                 */

                if (x <= 0) {
                    x += extraTapArea;
                }
                /*
                 * If result after calculating for extra tappable area is negative. assign the
                 * original value so that after subtracting extratapping area value doesn't go into
                 * negative value.
                 */

                if (y <= 0) {
                    y = actionY;
                }
                /**
                 * If drawble bounds contains the x and y points then move ahead.
                 */
                if (bounds.contains(x, y) && clickListener != null) {
                    clickListener.onClick(DrawableClickListener.DrawablePosition.RIGHT);
                    event.setAction(MotionEvent.ACTION_CANCEL);
                    return false;
                }
                return super.onTouchEvent(event);
            }

        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void finalize() throws Throwable {
        drawableRight = null;
        drawableBottom = null;
        drawableLeft = null;
        drawableTop = null;
        super.finalize();
    }

    /**
     * 
     * <br>
     * Created 2014年9月15日 下午11:04:58
     * 
     * @param listener DrawableClickListener
     * @author wat.ztag
     */
    public void setDrawableClickListener(DrawableClickListener listener) {
        this.clickListener = listener;
    }

    /**
     * 
     * <br>
     * Created 2014年9月15日 下午11:05:02
     * 
     * @version CustomEditText
     * @author wat.ztag
     * 
     * @see
     */
    public interface DrawableClickListener {

        /**
         * 
         * <br>
         * Created 2014年9月15日 下午11:05:08
         * 
         * @version CustomEditText.DrawableClickListener
         * @author wat.ztag
         * 
         * @see
         */
        public static enum DrawablePosition {
            TOP, BOTTOM, LEFT, RIGHT
        };

        /**
         * 
         * <br>
         * Created 2014年9月15日 下午11:05:13
         * 
         * @param target DrawablePosition
         * @author wat.ztag
         */
        void onClick(DrawablePosition target);
    }

}
